<template>
  <el-dialog
    class="add-menu-type-comp"
    :title="title"
    :close-on-click-modal="false"
    :destroy-on-close="true"
    :visible="visible"
    :width="$setRem(600)"
    :before-close="handleCancel">
    <div class="form-container">
      <el-form
        element-loading-custom-class="custom-load"
        :model="ruleForm"
        :rules="rules"
        size="medium"
        ref="ruleForm"
        :label-width="$setRem(120)"
        class="rule-form"
      >
        <el-row>
          <el-col :span="24">
            <el-form-item label="分类名称" prop="className" required>
              <el-input
                type="text"
                clearable
                placeholder="请输入分类名称"
                v-model="ruleForm.className">
              </el-input>
            </el-form-item>
          </el-col>
        </el-row>

        <el-row>
          <el-col :span="24">
            <el-form-item label="排序" prop="sort">
              <el-input
                type="text"
                clearable
                placeholder="请输入序号"
                v-model="ruleForm.sort">
              </el-input>
            </el-form-item>
          </el-col>
        </el-row>

        <el-row>
          <el-col :span="24">
            <el-form-item label="备注" prop="remark">
              <el-input
                type="textarea"
                :rows="3"
                placeholder="请输入备注"
                v-model="ruleForm.remark"
                clearable>
              </el-input>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
    </div>
    <span slot="footer" class="dialog-footer">
      <el-button size="medium" @click="handleCancel">取 消</el-button>
      <el-button
        size="medium"
        type="primary"
        :loading="loading"
        @click="hanldeSave">
        保 存
      </el-button>
    </span>
  </el-dialog>
</template>

<script>

export default {
  props: {
    // 弹框是否展示
    visible: {
      type: Boolean,
      default: false
    },
    option: {
      type: Object,
      default: () => ({})
    },
    loading: {
      type: Boolean,
      default: false
    },
    // 新增、编辑
    type: {
      type: String,
      default: 'add'
    }
  },
  data() {
    return {
      // form表单数据
      ruleForm: {
        id: '',
        className: '',
        sort: '',
        remark: ''
      },
      // form 表单校验规则
      rules: {
        className: [
          { required: true, message: '请输入分类名称' },
        ],
      },
    };
  },
  computed: {
    // 设置弹框标题
    title() {
      return `${this.type === 'add' ? '新增' : '编辑'}分类`;
    },
  },
  watch: {
    // 监听弹框显示/隐藏
    visible(value) {
      if (!value) {
        Object.keys(this.ruleForm).forEach(key => {
          this.ruleForm[key] = '';
        });
      }
    },
    // 回填数据
    option: {
      deep: true,
      handler: function (data) {
        const keys = Object.keys(this.ruleForm);
        data && Object.keys(data).length && keys.forEach(key => {
          this.ruleForm[key] = data[key] || '';
        });
      }
    }
  },
  methods: {
    // 保存
    hanldeSave() {
      this.$refs.ruleForm.validate(valid => {
        if (valid) {
          this.$emit('submit', this.ruleForm);
        }
      });
    },
    // 取消
    handleCancel() {
      this.$emit('cancel');
    }
  }
};
</script>

<style lang="scss" scoped>
  .add-menu-type-comp {
    .form-container {
      width: 100%;
      display: flex;
      align-items: center;
      padding-right: 20px;
      .el-form {
        width: 100%;
        .el-row {
          margin-bottom: 10px;
        }
      }
    }
  }
</style>
